<?php
class forumMod extends commonMod {
	public function __construct() {
		parent::__construct ();
	}
	public function index() {
		// 分页处理
		$url = __URL__ . '/index/page-{page}';
		$listRows = 6;
		// 每页的条数
		// $order = 'sort desc';
		$limit = $this->pagelimit ( $url, $listRows );
		$order = "release_time desc";
		$where = "1";
		if (! empty ( $_GET ['forum_id'] )) {
			$where .= " and forum_id='" . $_GET ['forum_id'] . "'";
			$url = __URL__ . '/index/page-{page}?forum_id=' . $_GET ['forum_id'];
			if (! empty ( $_GET ['type'] )) {
			if ( $_GET ['type']==3) {
				$order = "release_time desc";
				$url = __URL__ . '/index/page-{page}?new=' . $_GET ['type'].'&forum_id=' . $_GET ['forum_id'];
			}
			else {
				$where .= " and forum_type='" . $_GET ['type'] . "'and forum_id='" . $_GET ['forum_id'] . "'";
				$url = __URL__ . '/index/page-{page}?type=' . $_GET ['type'] .'&forum_id=' . $_GET ['forum_id'];
			}
			}
		} elseif (! empty ( $_GET ['type'] )) {
			if ( $_GET ['type']==3) {
				$order = "release_time desc";
				$url = __URL__ . '/index/page-{page}?new=' . $_GET ['type'].'&forum_id=' . $_GET ['forum_id'];
			}
			else {
			$where .= " and forum_type='" . $_GET ['type'] . "'";
			$url = __URL__ . '/index/page-{page}?type=' . $_GET ['type'];
			}
		}
		
		// 内容列表
		$count = $this->model->table ( "forum_cards_bbs" )->where ( $where )->count ();
		// 符合条件的总数
		if ($count>0){
		$list = $this->model->table ( "forum_cards_bbs" )->where ( $where )->order ( $order )->limit ( $limit )->select ();
		foreach ( $list as $key => $value ) {
			$info = $this->model->table ( 'forum_bbs' )->where ( " id = " . $value ['forum_id'] )->find ();
			$list [$key] ['cname'] = $info ['forum_name'];
			unset ( $info );
			//查询作者名称
			$forum_actor = $this->model->table ( 'uuser' )->where ( "uid=" . $value ['uid'] )->find ();
			$list [$key] ['forum_actor'] = $forum_actor ['nickname'];
			unset ( $forum_actor );
			
			// 计算发帖天数
			$time = time () - $list [$key] ['release_time'];
			$list [$key] ['isnew'] = intval ( round ( $time / 86400 ) );
			$list [$key] ['isjing'] = intval ( $list [$key] ['forum_type'] );
			
			$count_reply = $this->model->table ( "forum_reply_bbs" )->where ( "cards_id=" . $list [$key] ['id'] )->count (); // 符合条件的总数
			$list [$key] ['reply_num'] = $count_reply;
			
			if ($count_reply >= 1) {
				$last_time = $this->model->table ( "forum_reply_bbs" )->where ( "cards_id=" . $list [$key] ['id'] )->order ( 'id desc' )->find ();
				$list [$key] ['last_time'] = $last_time ['reply_time'];
				
				// 查询最后发表作者
				$last_reply = $info = $this->model->table ( 'uuser' )->where ( "uid=" . $last_time ['uid'] )->find ();
				$list [$key] ['last_reply'] = $last_reply ['nickname'];
				unset ( $last_reply );
				unset ( $count_reply );
				unset ( $last_time );
			}
			//排序 $list [$key] ['last_time']!=''判断是否0回复   $list [$key] ['isnew'] <100判断是否100天以前发表的帖子
			if ($list [$key] ['last_time']!=''&&$list [$key] ['isnew'] <100)
			{
				$list [$key] ['order']=$list [$key] ['last_time'];
			}
			else {
				$list [$key] ['order']=$list [$key] ['release_time'];
			}
		}
			if($_GET['type']!=3){
			//按最后回帖时间排序
			$list=$this->sortByMultiCols($list,array('last_time'=>SORT_DESC));
			}
		}
		$this->forum_list = $list;
		$this->page = $this->page ( $url, $count, $listRows );
		// 分页
		$this->type_list = $this->model->table ( 'forum_bbs' )->select ();
		$this->select ();
		$this->display ("forum_list.html");
		
	}
	public function forum_elevator() {
		$id = $_GET ['id'];
		$elevator = $_GET ['elevator'];
		$page = ceil ( $elevator / 2 );
		$this->redirect ( __URL__ . '/forum_show/page-' . $page . '-id-' . $id . '.html#replay_show_' . $elevator );
	}
	public function forum_show() {
		$id = $_GET ['id'];
		$uid = $_GET ['uid'];
		$page = $_GET ['page'];
		// 分页处理
		$url = __URL__ . '/forum_show/page-{page}.html?id=' . $id . '';
		$listRows = 2; // 每页的条数
		$limit = $this->pagelimit ( $url, $listRows );
		$this->tid = intval ( $_GET ['id'] );
		$where = " cards_id ='" . $id . "'";
		if ($uid) {
			$where .= " and uid = '" . $uid . "' ";
			$url = __URL__ . '/forum_show/page-{page}.html?id=' . $id . '&uid=' . $uid . '';
		}
		$count = $this->model->table ( "forum_reply_bbs" )->where ( $where )->count (); // 符合条件的总数

		//查询帖子详情
		$cards_info = $this->model->table ( "forum_cards_bbs" )->where ( "id ='" . $id . "'" )->find ();
		$member = $this->model->table ( "uuser" )->where ( "uid ='" . $cards_info ['uid'] . "'" )->find ();
		$info_title = $this->model->table ( 'forum_bbs' )->where ( " id = " . $cards_info ['forum_id'] )->find ();
		$cards_info ['cname'] = $info_title ['forum_name'];
		$cards_info ['nickname'] = $member ['nickname'];
		//发布者发布帖子总数
		$cards_info  ['ucards_count'] = $this->model->table ( 'forum_cards_bbs' )->where ( "uid ='" . $member ['uid'] . "'" )->count ();

		$this->page = $this->page ( $url, $count, $listRows ); // 分页
		if (! $page) {
			if(!isset($_SESSION['uuser'][$cards_info['title']])){
				$_SESSION['uuser'][$cards_info['title']]=$cards_info['id'];
				$cards_info ['browse_num'] = $cards_info ['browse_num'] + 1;
				$this->model->table('forum_cards_bbs')->data($cards_info ['browse_num'])->where("  id ='" . $id . "' ")->update(); // 浏览次数
			}
		}
		//查询回复
		$reply_list = $this->model->table ( "forum_reply_bbs" )->where ( $where)->limit ( $limit )->select ();
		if ($reply_list) {
			foreach ( $reply_list as $k => $b ) {
				$member = $this->model->table ( "uuser" )->where ( "uid ='" . $b ['uid'] . "'" )->find ();
				$reply_list [$k] ['user_name'] = $member ['nickname'];
				$reply_list [$k] ['hfcount'] = $this->model->table ( 'forum_cards_bbs' )->where ( "uid ='" . $b ['uid'] . "'" )->count ();
				unset ( $member );
			}
		}
		$this->reply_list = $reply_list;
		$this->cards_info = $cards_info;
		$count = $this->model->table ( 'forum_reply_bbs' )->where ( "cards_id ='" . $id . "'" )->count ();
		$this->count = $count;
		$floor = $this->model->table ( 'forum_reply_bbs' )->where ( "cards_id='" . $id . "'" )->count ();
		$this->floor = $floor;
		$this->type_list = $this->model->table ( 'forum_bbs' )->select ();
		$this->show_id = $id;
		$this->select ();
		$this->display ( 'forum_show.html' );
	}
	public function forum_only() {
		$id = $_GET ['uid'];
		$list = $this->model->table ( "forum_reply_bbs" )->where ( "uid ='" . $id . "'" )->select ();
		if ($replay_list) {
			foreach ( $list as $k => $b ) {
				$member = $this->model->table ( "uuser" )->where ( "uid ='" . $b ['uid'] . "'" )->find ();
				$list [$k] ['u_name'] = $member ['name'];
				/* $replay_list[$k]['user_head'] = $member['name']; */
				unset ( $member );
			}
		}
		$this->list = $list;
		$this->redirect ( __URL__ . "/forum_show?uid=" . $id );
	}
	public function replay_save() {
		$id = $_POST ['cards_id'];
		if (! $_SESSION ['uuser']) {
			$this->alert ( "请先登录", __INDEX__ . "/user/login" );
		} else {
			$data ['cards_id'] = $id;
			$data ['floor'] = $_POST ['floor'] + 1;
			$data ['forum_textrea'] = $_POST ['r_content'];
			$data ['uid'] = $_SESSION ['uuser'] [uid];
			$data ['reply_time'] = time ();
			$this->model->table ( "forum_reply_bbs" )->data ( $data )->insert ();
			$this->redirect ( __URL__ . "/forum_show?id=" . $id );
		}
	}
	public function report() {
		$id = $_GET ['id'];
		$data ['forum_id'] = $id;
		$forum_title = $this->model->table ( "forum_cards_bbs" )->field ( 'forum_title' )->where ( "id ='" . $id . "' " )->find ();
		$data ['forum_title'] = $forum_title ['forum_title'];
		$data ['reply_id'] = $_GET ['rid'];
		$data ['forum_uid'] = $_SESSION ['uuser'] [uid];
		$data ['report_time'] = time ();
		$this->model->table ( "forum_report_bbs" )->data ( $data )->insert ();
		$this->alert ( "举报成功", __URL__ . "/forum_show?id=" . $id );
	}
	public function add_save() {
	//	dump( $_POST ['mod'],true);
		if (! empty ( $_POST ['title'] )) {
			$data ['forum_title'] = $_POST ['title'];
		} else {
			$this->alert ( "标题不能为空" );
		}
		if (empty ( $_SESSION ['uuser'] )) {
			$this->alert ( "请先登录", __INDEX__ . "/user/login" );
		} else {
			$data ['uid'] = $_SESSION ['uuser'] [uid];
		}
		if (! empty ( $_POST ['content'] )) {
			$data ['forum_content'] = $_POST ['content'];
		} else {
			$this->alert ( "内容不能为空" );
		}
		$data ['release_time'] = time ();
		if (! empty ( $_POST ['mod'] )) {
			$data ['forum_id'] = $_POST ['mod'];
		} else {
			$this->alert ( "主题不能为空" );
		}
		$insertid = $this->model->table ( "forum_cards_bbs" )->data ( $data )->insert ();
		$this->redirect ( __URL__ . "/forum_show?id=" . $insertid );
	}
	// 我的收藏
	// 收藏帖子
	public function forum_collection() {
		// 判断是否登录
		if (empty ( $_SESSION ['uuser'] [uid] )) {
			$this->alert ( "请登录", __INDEX__ );
		}
		// 判断是否收藏帖子
		$info_id = $_GET ['id'];
		$check = $this->model->table ( 'forum_collection_bbs' )->where ( "cards_id =" . $info_id . " and uid =" . $_SESSION ['uuser'] [uid] )->find ();
		if ($check) {
			$this->model->table ( 'forum_collection_bbs' )->where ( "id = " . $check ['id'] )->delete ();
			$cishu = $this->model->table ( 'forum_cards_bbs' )->field ( 'collection_num' )->where ( "id=" . $info_id )->find ();
			$cishu ['collection_num'] --;
			$this->model->table ( 'forum_cards_bbs' )->data ( $cishu )->where ( "id=" . $info_id )->update ();
			$this->alert ( "取消成功", __URL__ . "/collection_list" );
		} else {
			$data ['cards_id'] = $info_id;
			$data ['uid'] = $_SESSION ['uuser'] [uid];
			$data ['insert_time'] = time ();
			$this->model->table ( 'forum_collection_bbs' )->data ( $data )->insert ();
			// 增加收藏次数
			$cishu = $this->model->table ( 'forum_cards_bbs' )->field ( 'collection_num' )->where ( "id=" . $info_id )->find ();
			$cishu ['collection_num'] ++;
			$this->model->table ( 'forum_cards_bbs' )->data ( $cishu )->where ( "id=" . $info_id )->update ();
			$this->alert ( "收藏成功", __URL__ . "/collection_list" );
		}
		$this->type_list = $this->model->table ( 'forum_bbs' )->select ();
	}
	
	// 查询帖子
	public function collection_list() {
		// 分页处理
		$url = __URL__ . '/collection_list/page-{page}';
		$listRows = 4; // 每页的条数
		$limit = $this->pagelimit ( $url, $listRows );
		// 获取帖子条数
		$count = $this->model->table ( "forum_collection_bbs" )->where ( "uid ='".$_SESSION ['uuser'] ['uid']."'"   )->count (); // 符合条件的总数
		if($count>0) {
			$this->page = $this->page($url, $count, $listRows); // 分页
			// 查询帖子显示帖子内容
			$list = $this->model->table('forum_collection_bbs')->where("uid = " . $_SESSION ['uuser'] ['uid'])->limit($limit)->select();
			if ($list) {
				foreach ($list as $k => $v) {
					$content = $this->model->table('forum_cards_bbs')->where("id=" . $v ['cards_id'])->order('release_time desc')->find();
					$list [$k] ['title'] = $content ['forum_title'];
					$list [$k] ['content'] = $content ['forum_content'];
					unset ($content);
				}
				$this->list = $list;
			}
		}
		$this->type_list = $this->model->table('forum_bbs')->select();
		$this->select();
		$this->count = $count;
		$this->display ( "forum_collection.html" );
	}
	// 下拉框显示内容
	public function select() {
		$xiala = $this->xiala = $this->model->table ( 'forum_cards_bbs' )->where ( "forum_type='2'" )->select ();
		return $xiala;
	}
	
	// 搜索帖子
	public function search() {
		$url = __URL__ . '/collection_list/page-{page}';
		$listRows = 4; // 每页的条数
		$limit = $this->pagelimit ( $url, $listRows );
		$title_cn = urldecode ( $_GET ['sousuo'] );
		$list = $this->model->table ( 'forum_cards_bbs' )->where ( "forum_title like '%" . $title_cn . "%'" )->select ();
		if (empty ( $list )) {
			$this->alert ( "没有相关帖子" );
		} else {
			foreach ( $list as $k => $t ) {
				$list [$k] ['title'] = $title ['forum_title'];
			}
			$this->list = $list;
		}
		
		// 查询帖子显示帖子内容
		if (! empty ( $_SESSION ['uuser'] [uid] )) {
			$list = $this->model->table ( 'forum_collection_bbs' )->where ( "uid = " . $_SESSION ['uuser'] [uid] )->limit ( $limit )->select ();
			if ($list) {
				foreach ( $list as $k => $v ) {
					$content = $this->model->table ( 'forum_cards_bbs' )->where ( "id=" . $v ['cards_id'] )->order ( 'release_time desc' )->find ();
					$list [$k] ['title'] = $content ['forum_title'];
					$list [$k] ['content'] = $content ['forum_content'];
					unset ( $content );
				}
				$this->list_title = $list;
			}
		}
		$this->select ();
		$this->display ( "forum_search.html" );
	}
	
	/* 二维数组排序
	 * @param array $rowset 要排序的数组
	 * @param array $args 排序的键（array('insert_time'=>SORT_DESC)）
	 */
	function sortByMultiCols($rowset, $args) {
		$sortArray = array();
		$sortRule = '';
		foreach ($args as $sortField => $sortDir) {
			foreach ($rowset as $offset => $row) {
				$sortArray[$sortField][$offset] = $row[$sortField];
			}
			$sortRule .= '$sortArray[\'' . $sortField . '\'], ' . $sortDir . ', ';
		}
		if (empty($sortArray) || empty($sortRule)) {
			return $rowset;
		}
		eval('array_multisort(' . $sortRule . '$rowset);');
		return $rowset;
	}
	
}